<template>
  <div class="mod-attrgroup">
    <el-container>
      <el-aside style="width: 20%; min-width: 200px">
        <el-card class="box-card">
          <template #header>
            <div class="clearfix">
              <span class="a-title">商品分类</span>
            </div>
          </template>
          <category-tree @node-click="treeNodeClickEvent"></category-tree>
        </el-card>
      </el-aside>
      <el-main style="padding: 0 20px">
        <el-card v-if="table.visible" class="box-card">
          <template #header>
            <div class="clearfix">
              <span class="a-title">商品分类[{{ tree.selectData.name }}]-分组列表</span>
            </div>
          </template>
          <data-table ref="dataTableRef" name="attrgroup" :initLoad="true" :pagination="false" :url="table.url">
            <!-- ------------------------------------------ 工具栏 ------------------------------------------ -->
            <template v-slot:toolbar>
              <el-button-group>
                <el-button v-if="admin.isAuth('product:attrgroup:save')" @click="() => addAttrgroupEvent()" type="primary">
                  新增
                </el-button>
                <el-button v-if="admin.isAuth('product:attrgroup:save')" @click="() => updateAttrgroupEvent()" type="warning">
                  修改
                </el-button>
                <el-button v-if="admin.isAuth('product:attrgroup:delete')" @click="() => deleteAttrgroupEvent()" type="danger">
                  删除
                </el-button>
              </el-button-group>
            </template>
            <!-- ---------------------------------------- 数据表格列 ---------------------------------------- -->
            <el-table-column fixed prop="name" show-overflow-tooltip header-align="center" align="center" label="组名" width="180" />
            <el-table-column prop="icon" header-align="center" align="center" label="图标" width="120">
              <template #default="scope">
                <icon-svg v-if="scope.row.icon" :name="scope.row.icon"></icon-svg>
              </template>
            </el-table-column>
            <el-table-column prop="sort" header-align="center" align="center" label="排序" width="120" />
            <el-table-column prop="desct" show-overflow-tooltip header-align="center" align="left" label="介绍" />
            <el-table-column fixed="right" header-align="center" align="center" width="100" label="操作">
              <template #default="scope">
                <el-button size="small" @click="attrgroupAttrRelationEvent(scope.row)">关联</el-button>
              </template>
            </el-table-column>
          </data-table>
        </el-card>
      </el-main>
    </el-container>
    <!-- 弹窗, 新增 / 修改 -->
    <attrgroup-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateRef" @loadAttrgroupData="loadAttrgroupData"></attrgroup-add-or-update>
    <attrgroup-attr-relation v-if="attrgroupAttrRelationVisible" ref="attrgroupAttrRelationRef"></attrgroup-attr-relation>
  </div>
</template>

<script lang="ts" setup>
import admin from "@/utils/admin";
import http from "@/utils/httpRequest";
import { ref, reactive, watch, nextTick } from "vue";
import cloneDeep from "lodash/cloneDeep";
import { ElTree, ElMessage, ElMessageBox } from "element-plus";

import CategoryTree from "../common/category-tree.vue";
import AttrgroupAddOrUpdate from "./attrgroup-add-or-update.vue";
import AttrgroupAttrRelation from "./attrgroup-attr-relation.vue";

// 树属性
const tree = reactive({
  // 当前树选中的节点
  selectData: undefined,
});

// 表格相关属性
const table = reactive({
  // 是否显示
  visible: false,
  // 请求地址
  url: "",
});

// 新增修改弹窗是否显示
const addOrUpdateVisible = ref(false);

// 关联窗口是否显示
const attrgroupAttrRelationVisible = ref(false);

// 数据表格引用
const dataTableRef = ref();

// 弹窗引用
const addOrUpdateRef = ref();

// 关系窗口引用
const attrgroupAttrRelationRef = ref();

// 分类节点点击事件
const treeNodeClickEvent = (data) => {
  tree.selectData = data;
  if (data.attributes && data.attributes.level === 3) {
    loadAttrgroupData();
  }
};

// 初始化属性分组列表
const loadAttrgroupData = async () => {
  if (!tree.selectData) {
    return;
  }
  table.url = `/product/attrgroup/list?categoryId=${tree.selectData.id}`;
  table.visible = true;
  await nextTick();
  dataTableRef.value.loadTableData();
};

// 新增属性分组
const addAttrgroupEvent = async () => {
  if (!tree.selectData) {
    ElMessage.error("请先选择新增属性分组所属分类！");
    return;
  }
  let dataList = dataTableRef.value.getDataList();
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("add", dataList, tree.selectData);
};

// 修改属性分组
const updateAttrgroupEvent = async () => {
  if (!tree.selectData) {
    ElMessage.error("请先选择新增属性分组所属分类！");
    return;
  }
  let data = admin.obtainOneSelectionTableData(dataTableRef.value);
  if (!data) {
    return;
  }
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("update", data, tree.selectData);
};

// 删除属性分组
const deleteAttrgroupEvent = () => {
  admin.batchDeleteSelectionTableData(dataTableRef.value, "/product/attrgroup/delete");
};

// 打开属性分组分类关联窗口
const attrgroupAttrRelationEvent = async (data) => {
  attrgroupAttrRelationVisible.value = true;
  await nextTick();
  attrgroupAttrRelationRef.value.initPage({
    categoryId: tree.selectData.id,
    attrgroupId: data.id,
    attrgroupName: data.name,
  });
};
</script>

<style lang="scss" scoped>
.mod-attrgroup {

  .a-title {
    font-size: 16px;
    font-weight: 600;
  }
}
</style>